/*
 * Author: vyouzhi <vyouzhi@163.com>
 * http://www.xda.cn
 *
 * File: token_lib.c
 * Create Date: 2011-10-19 10:53:33
 *
 */
#include "token_lib.h"

char *GetSQL(int num){
	int fd, len;
	char init_sql[8129], itc[2], *isql;
	
	fd = open("./sql.sql", O_RDONLY);
	assert((fd!=0));

	len = read(fd, init_sql, 8129);

	sprintf(itc, "%d", num);
	init_sql[21] = itc[0];
	close(fd);

	init_sql[len]='\0';

	isql = strdup(init_sql);

	return isql;
}

int token_init(void){
	MYSQL *con;
	char *sql, db_name[1024], init_tables[]="log_%";
	int n, i;

	con = init_mysql();
	assert((con != NULL));

	sprintf(db_name, "use %s;", conf_get("log_db"));

	if(-1 == query_mysql(db_name, con)){
		close_mysql(con);
		exit(-1);
	}

	if(-1 == list_table(init_tables, con)){
		n = atoi(conf_get("table_num"));
		for(i=0; i<n; i++){
			sql = GetSQL(i);
			if(-1 == query_mysql(sql, con)){
				free(sql);
			 	continue;
			}
			commit_mysql(con);
			free(sql);
		}
	}

	close_mysql(con);

    return 0;
}
/* vim: set ts=4 sw=4: */

